﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectStorageDB;

namespace ProjectStorageLib
{
    public abstract class DatabaseEntities : IDisposable
    {
        #region Variables
        public ProjectStorageEntities ent = null;
        private bool dispose = true;
        #endregion

        #region Constructor and Destructor
        /// <summary>
        /// Cosntructor of the class
        /// </summary>
        public DatabaseEntities()
        {
            try
            {
                this.ent = MainFunction.GetProjectStorageEntities();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DatabaseEntities(DatabaseEntities parent)
        {
            this.ent = parent.ent;
            dispose = true;
        }

        /// <summary>
        /// A method to realize IDisposable interface
        /// </summary>
        public void Dispose()
        {
            if (ent != null && dispose)
                ent.Dispose();
            //Call internal dispose method
            Dispose(true);

            //Disconnect from garbage collection finalize queue
            System.GC.SuppressFinalize(this);
        }

        /// <summary>
        /// A Destructor of the class
        /// </summary>
        ~DatabaseEntities()
        {
            //Call internal dispose method
            Dispose(false);
        }

        /// <summary>
        /// A method to be realized in successors for implementing 
        /// resourse and memory disposal.
        /// </summary>
        /// <param name="flag">flag marking if the Dispose is triggered by user or by system </param>
        protected abstract void Dispose(bool flag);
        #endregion
    }
}
